/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TransformControl.h"

#include <QtGui/QLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>

namespace PQBuilder
{
	TransformControl::TransformControl(QWidget *parent,PQEngine::TransformComponent* component)
	{
		if(component!=0){
			QGridLayout* grid=new QGridLayout(this);
			
			//position 
			QLabel* labelPosition=new QLabel(QObject::tr("Position"),this);
			QLabel* labelRotation=new QLabel(QObject::tr("Rotation"),this);
			QLabel* labelScale=new QLabel(QObject::tr("Scale"),this);

			Ogre::Vector3 pos=component->getPosition();
			
			_lineEditPosX=new NumericTextEdit(pos.x);
			_lineEditPosY=new NumericTextEdit(pos.y);
			_lineEditPosZ=new NumericTextEdit(pos.z);

			//rotation 
			Ogre::Quaternion q=component->getOrientation();
			Ogre::Real rotX=q.getPitch().valueDegrees();
			Ogre::Real rotY= q.getYaw().valueDegrees();
			Ogre::Real rotZ=q.getRoll().valueDegrees();

			_lineEditRotX=new NumericTextEdit(rotX);
			_lineEditRotY=new NumericTextEdit(rotY);
			_lineEditRotZ=new NumericTextEdit(rotZ);

			//scale 
			Ogre::Vector3 scale=component->getScale();

			_lineEditScaleX=new NumericTextEdit(scale.x);
			_lineEditScaleY=new NumericTextEdit(scale.y);
			_lineEditScaleZ=new NumericTextEdit(scale.z);

			grid->addWidget(labelPosition,0,0);

			grid->addWidget(_lineEditPosX,1,0);
			grid->addWidget(_lineEditPosY,1,1);
			grid->addWidget(_lineEditPosZ,1,2);
			
			grid->addWidget(labelRotation,2,0);

			grid->addWidget(_lineEditRotX,3,0);
			grid->addWidget(_lineEditRotY,3,1);
			grid->addWidget(_lineEditRotZ,3,2);

			grid->addWidget(labelScale,4,0);

			grid->addWidget(_lineEditScaleX,5,0);
			grid->addWidget(_lineEditScaleY,5,1);
			grid->addWidget(_lineEditScaleZ,5,2);

			_controlHeight=grid->rowCount()*28+12;
			this->setLayout(grid);
			this->setMaximumHeight(_controlHeight);
		}
	}

	TransformControl::~TransformControl()
	{

	}

	void TransformControl::OnTranslateObject(PQEngine::GameObject *object)
	{
		PQEngine::TransformComponent* component=object->getTransformComponent();
		Ogre::Vector3 pos=component->getPosition();
		_lineEditPosX->setFloat(pos.x);
		_lineEditPosY->setFloat(pos.y);
		_lineEditPosZ->setFloat(pos.z);

	}

	void TransformControl::OnRotateObject(PQEngine::GameObject *object)
	{
		PQEngine::TransformComponent* component=object->getTransformComponent();
		Ogre::Quaternion q=component->getOrientation();
		Ogre::Real rotX=q.getPitch().valueDegrees();
		Ogre::Real rotY= q.getYaw().valueDegrees();
		Ogre::Real rotZ=q.getRoll().valueDegrees();
		_lineEditRotX->setFloat(rotX);
		_lineEditRotY->setFloat(rotY);
		_lineEditRotZ->setFloat(rotZ);
	}

	void TransformControl::OnScaleObject(PQEngine::GameObject *object)
	{
		PQEngine::TransformComponent* component=object->getTransformComponent();
		Ogre::Vector3 scale=component->getScale();
		_lineEditScaleX->setFloat(scale.x);
		_lineEditScaleY->setFloat(scale.y);
		_lineEditScaleZ->setFloat(scale.z);
	}

}
